package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.PartsManagement.ComponentItemClassification;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessTypeDao;
import com.cn.mapper.dao.db5.PartsManagement.ComponentItemClassificationDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.ComponentItemClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ComponentItemClassificationServiceImpl implements ComponentItemClassificationService {
    private final ComponentItemClassificationDao componentItemClassificationDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public ComponentItemClassificationServiceImpl(ComponentItemClassificationDao componentItemClassificationDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.componentItemClassificationDao = componentItemClassificationDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }

    @Override
    public List<ComponentItemClassification> selectComponentItemClassificationAside() {
        return componentItemClassificationDao.selectComponentItemClassificationAside();
    }

    @Override
    public List<ComponentItemClassification> selectComponentItemClassificationText(String cCode, String cName) {
        return componentItemClassificationDao.selectComponentItemClassificationText(cCode, cName);
    }

    @Override
    public List<ComponentItemClassification> selectComponentItemClassification(int limit, int page, String cCode, String cName) {
        return componentItemClassificationDao.selectComponentItemClassification(limit, page, cCode, cName);
    }

    @Override
    public Result addComponentItemClassificationAside(String mName, String mDescription, int mStatus, int parentId, int level, int isLeaf) {
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectAside();
        for (ComponentItemClassification componentItemClassification : componentItemClassifications) {
            if (componentItemClassification.getMName().equals(mName)) {
                return Result.failure("500", "项目分类名称重复");
            }
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ComponentItemClassificationTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {

            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "ComponentItemClassificationTable");


        int i = componentItemClassificationDao.addComponentItemClassificationAside(ID, code, mName, mDescription, mStatus, parentId, level, 0);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result addComponentItemBasicInfoTable(String cName, String cDescription, int cStatus, int classificationId) {
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectChild();
        for (ComponentItemClassification componentItemClassification : componentItemClassifications) {
            if (componentItemClassification.getCName().equals(cName)) {
                return Result.failure("500", "项目基础信息名称重复");
            }
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ComponentItemBasicInfoTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {

            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "ComponentItemBasicInfoTable");
        int i = componentItemClassificationDao.addComponentItemBasicInfoTable(ID, code, cName, cDescription, cStatus, classificationId);

        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public List<ComponentItemClassification> selectAside() {
        return componentItemClassificationDao.selectAside();
    }

    @Override
    public Result deleteAside(int mId) {
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectAside();
        boolean hasChildren = hasChildren(componentItemClassifications, mId);
        if (hasChildren) {
            return Result.failure("500", "删除失败，当前节点存在子节点");
        }
        int i = componentItemClassificationDao.deleteAside(mId);
        if (i == 0) {
            return Result.failure("500", "删除失败");
        }
        return Result.success("200", "删除成功");
    }

    @Override
    public Result deleteComponentItemBasicInfo(int cId) {
        List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectChild();

        int i = componentItemClassificationDao.deleteComponentItemBasicInfo(cId);
        if (i == 0) {
            return Result.failure("500", "删除失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result updateAside(int mId, String mName, String mDescription, int mStatus, int parentId, int level) {
        int i = componentItemClassificationDao.updateAside(mId, mName, mDescription, mStatus, parentId, level);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        return Result.success("200", "修改成功");
    }

    @Override
    public Result updateComponentItemBasicInfo(int cId, String cName, String cDescription, int cStatus, int classificationId) {
        int i = componentItemClassificationDao.updateComponentItemBasicInfo(cId, cName, cDescription, cStatus, classificationId);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        return Result.success("200", "修改成功");
    }

    @Override
    public List<ComponentItemClassification> selectChild() {
        return componentItemClassificationDao.selectChild();
    }

    @Override
    public List<ComponentItemClassification> selectChildAuto1(String cName) {

        return componentItemClassificationDao.selectChildAuto1(cName);
    }

    private boolean hasChildren(List<ComponentItemClassification> nodes, int mId) {
        for (ComponentItemClassification node : nodes) {
            if (node.getParentId() == mId) {
                return true;
            }
        }
        return false;
    }

}
